
'use strict';

stareal
    .controller("ActiveSearchController", function ($scope, $api, $stateParams, $timeout, $lazyLoader,localStorageService,$alert,$location,$anchorScroll) {
        // 展现热词
        $scope.hwf = true;
        var h=$(window).height()-2.4*parseFloat(document.documentElement.style.fontSize)+"px";
        $("#wrapper_container").height(h);
        if(localStorageService.get('cityid')==undefined||localStorageService.get('cityid')==null){
            $scope.cityid='';
            $scope.cityName='全国';
        }else{
            $scope.cityid=localStorageService.get('cityid');
            $scope.cityName=localStorageService.get('cityname');
        }
        $scope.selectID=0;
        $scope.stauts1=false;
        // $scope.stauts2=false;
        $scope.stauts3=false;
        // $scope.text2='全部';
        // $scope.activeStatus=1;
        $scope.text3='全部类别';
        $scope.activeType=-1;
        if($scope.cityid==''){
            $scope.text1='全国';
        }else{
            $scope.text1=$scope.cityName;
        }
        $scope.history_list =new Array(8);
        if(localStorageService.get("activeSearchList")==''||localStorageService.get("activeSearchList")==null||localStorageService.get("activeSearchList")==undefined){
            $scope.history_list=[];
        }else{
            $scope.history_list = JSON.parse(localStorageService.get("activeSearchList"));
        }
        if($scope.history_list){
            $scope.history = $scope.history_list;
        }else{
            $scope.history = [];
            $scope.hwf=false;
        }

        // console.log($scope.history)
        // $scope.history_list;
        //搜索方法
        var search = function(keyword){
            $timeout(function () {
                $scope.hwf = false;
                $scope.hwe = false;
                $scope.goods = new $lazyLoader("app/activity/searchActivity", {
                    searchValue: keyword,
                    classifyType:$scope.activeType,
                    cityName: $scope.cityName

                });
                $scope.goods.nextPage();
            }, 0);
        };
        //点击热词搜索
        $scope.hws = function(keyword){
            $scope.keyword = keyword;
            search(keyword);
        };
        //回车搜索
        var searchBtn = document.getElementById("searchBtn");
        var sou_iocnBtn1=document.getElementById('sou_iocnBtn1');
        var searchClear_icon1=document.getElementById('searchClear_icon1');
        searchBtn.onsearch = function(event){
            var keyword = this.value;
            event.preventDefault();
            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search(keyword);
                $scope.history.unshift({"word": keyword});
                localStorageService.set("activeSearchList", JSON.stringify($scope.history));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }
        };
        //输入没有内容
        searchBtn.oninput = function () {
            if(this.value.length<1){
                $scope.hwe = true;
                $scope.hwf = true;
                $scope.goods.nodata = false;
                $scope.history_list = JSON.parse(localStorageService.get("activeSearchList"));
            }
        }
        sou_iocnBtn1.onclick=function(){
            var keyword = $('#searchBtn').val();
            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search(keyword);
                $scope.history.unshift({"word": keyword});
                localStorageService.set("activeSearchList", JSON.stringify($scope.history));
            }else{
                $alert.show('搜索内容不能为空!');
            }
        }
        searchClear_icon1.onclick=function(){
            $('#searchBtn').val('');
        }
        $scope.dele = function (id) {
            $scope.history_list.splice(id,1 );
            $scope.history.splice(id,1 );
            localStorageService.set("activeSearchList",JSON.stringify($scope.history_list));
        }
        $scope.clearAll=function(){
            $scope.history_list=[];
            $scope.history=[];
            localStorageService.set("activeSearchList",'');
        }
        // localStorageService.remove("list")
        //筛选

        $scope.getCureenCity=function(){
            $api.get("app/city/list",{}, true)
                .then(function(ret){
                    $scope.saiCities=ret.data;
                })
        }
        $scope.getCureenCity();
        $scope.getBarNavs=function(){
            // 分类列表
            $api.get("app/activity/classifylist",{}, true)
                .then(function(ret){
                    var data=ret.data;
                    $scope.barNavs=data;
                },function(err){
                    $alert.show(err);
                });
        }
        $scope.getBarNavs();
        //选中筛选的条件
        //    下拉分类
        $scope.switchnavSelect=function(idnx,status){
            $scope.selectID=idnx;
            if($scope.selectID==1){
                $scope.stauts3=false;
                if($scope.stauts1==false){
                    $scope.stauts1=true;
                }else{
                    $scope.stauts1=false;
                }
            }else if($scope.selectID==3){
                $scope.stauts1=false;
                if($scope.stauts3==false){
                    $scope.stauts3=true;
                }else{
                    $scope.stauts3=false;
                }
            }
        }
        //选中筛选的条件
        $scope.changesnav=function(idnx,text,cid){
            $scope.selectID=idnx;
            if($scope.selectID==1){
                $scope.stauts1=false;
                $scope.stauts3=false;
                $scope.text1=text;
                $scope.cityid=cid;
                $scope.cityName=text;
              //   localStorageService.set('cityid',cid);
              // localStorageService.set('cityname',text);
                var keyword = $('#searchBtn').val();
                if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                    search(keyword);
                    $scope.history.unshift({"word": keyword});
                    localStorageService.set("activeSearchList", JSON.stringify($scope.history));
                }else{
                    $alert.show('搜索内容不能为空!');
                }
            }else if($scope.selectID==3){
                $scope.stauts1=false;
                $scope.stauts3=false;
                $scope.text3=text;
                if(cid==-1){
                    $scope.activeType=-1
                }else{
                    $scope.activeType= $scope.barNavs[cid].key;
                }
                var keyword = $('#searchBtn').val();
                if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                    search(keyword);
                    $scope.history.unshift({"word": keyword});
                    localStorageService.set("activeSearchList", JSON.stringify($scope.history));
                }else{
                    $alert.show('搜索内容不能为空!');
                }
            }
        }
        $scope.sortscity=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U','V','W','X','Y','Z'];
        $scope.gotoBottom = function(id) {
            //   console.log(id);
            // 将location.hash的值设置为
            // 你想要滚动到的元素的id
            $location.hash(id);

            // 调用 $anchorScroll()
            $anchorScroll();
        };
        $scope.goToOther=function(url){
            if(url!=''&&url!=undefined&&url!=null){
                window.open(url);
            }
        }
        //兑吧自定义活动
        $scope.goToOtherDui=function(url){
            if(!localStorageService.get('token')){
                $scope.activeDuiDailogStatus=true;
                return false;
            }
            if(url!=''&&url!=undefined&&url!=null){
                $scope.venus360Id=url+'&isApp=1';
                $api.get("app/duiba/shareH5",{dbredirect:$scope.venus360Id},true)
                    .then(function (ret) {
                        var data=ret.data;
                        console.log(data);
                        var link=data.url;
                        console.log(link);
                        window.location.href=link;
                        // window.open(link);
                        //window.open('https://activity.m.duiba.com.cn/chome/index?from=login&spm=53694.1.1.1','_blank');
                    })
            }
        }
        $scope.closedActiveDailog=function(){
            $scope.activeDuiDailogStatus=false;
        }
        $scope.goTo=function(){
            var  rs = "main.newactivelist-" + JSON.stringify({'kind':$scope.currentIndex,activenavbar:$scope.activenavbar});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                if (isbind != 1 && openID != '') {
                    localStorageService.set('cleartoken', token);
                    location.href = "#/main/register/" + encodeURIComponent(rs);
                    localStorageService.set('token', '');
                    return false
                }
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.newactivelist-" + JSON.stringify({'kind':$scope.currentIndex,activenavbar:$scope.activenavbar});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs)+"&connect_redirect=1#wechat_redirect";
                } else {
                    console.log(encodeURIComponent(rs));
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
        }
    });